package com.hctforgreen.greenservice.ui.widget;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.hctforgreen.greenservice.R;
import com.hctforgreen.greenservice.TerminalExamActivity;
import com.hctforgreen.greenservice.TerminalLearningMaterialsActivity;
import com.hctforgreen.greenservice.model.MessageTopThirdEntity;
import com.hctforgreen.greenservice.ui.adapter.MessageTopThirdLstAdapter;
import com.hctforgreen.greenservice.ui.adapter.TerminalDataTimeListAdapter;

/**
 * 
 * @author 390694 终端培训---时间选择下拉框
 * 
 */
public class TerminalExamineTimePopupView extends PopupWindow {

	private View mConvertView;
	private Activity mActivity;

	private MessageTopThirdEntity mSelectTimeEntity;

	private ArrayList<MessageTopThirdEntity> mEntities = new ArrayList<MessageTopThirdEntity>();

	private TerminalExamActivity mSelf2;

	public TerminalExamineTimePopupView(Activity mActivity,
			MessageTopThirdEntity mSelectTimeEntity, TerminalExamActivity mSelf2) {

		super(mActivity);
		this.mActivity = mActivity;

		this.mSelectTimeEntity = mSelectTimeEntity;
		this.mSelf2 = mSelf2;

		initDataList();
		initWindow();

	}

	/**
	 * 
	 * @param type
	 *            0:最近一周；1：最近一个月；2：最近一年
	 * @return
	 */
	private String getDateFrom(int type) {
		java.util.Date date = new java.util.Date();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		long lTime;
		if (type == 0) {
			lTime = (date.getTime() / 1000) - 60 * 60 * 24 * 7;
			date.setTime(lTime * 1000);
			String sDateString = format.format(date);
			return sDateString;
		} else if (type == 1) {
			lTime = (date.getTime() / 1000) - 60 * 60 * 24 * 30;
			date.setTime(lTime * 1000);
			String sDateString = format.format(date);
			return sDateString;
		} else if (type == 2) {
			lTime = (date.getTime() / 1000) - 60 * 60 * 24 * 365;
			date.setTime(lTime * 1000);
			String sDateString = format.format(date);
			return sDateString;
		} else {
			return "";
		}
	}

	private void initDataList() {
		MessageTopThirdEntity timeEntity = new MessageTopThirdEntity();
		timeEntity.name = mActivity.getResources().getString(
				R.string.terminal_train_one_week);
		timeEntity.dateFrom = getDateFrom(0);
		timeEntity.dateType = "0";
		mEntities.add(timeEntity);

		timeEntity = new MessageTopThirdEntity();
		timeEntity.name = mActivity.getResources().getString(
				R.string.terminal_train_one_meonth);
		timeEntity.dateFrom = getDateFrom(1);
		timeEntity.dateType = "1";
		mEntities.add(timeEntity);

		timeEntity = new MessageTopThirdEntity();
		timeEntity.name = mActivity.getResources().getString(
				R.string.terminal_train_one_year);
		timeEntity.dateFrom = getDateFrom(2);
		timeEntity.dateType = "2";
		mEntities.add(timeEntity);
	}

	private void initWindow() {
		mConvertView = getLayout();

		this.setContentView(mConvertView);
		this.setBackgroundDrawable(new BitmapDrawable());
		this.setOutsideTouchable(true);
		this.setFocusable(true);
		this.setWidth(LayoutParams.FILL_PARENT);
		this.setHeight(LayoutParams.WRAP_CONTENT);

		loadDataList();
	}

	private void loadDataList() {
		final ListView firstLv = (ListView) mConvertView
				.findViewById(R.id.lst_default_list);

		renderDataLst(firstLv, mEntities);
	}

	private void renderDataLst(ListView firstLv,
			List<MessageTopThirdEntity> entities) {

		if (entities == null || entities.size() == 0) {
			return;
		}

		new TerminalDataTimeListAdapter(firstLv, mActivity, entities,
				mSelectTimeEntity);
	}

	private View getLayout() {
		View layout = mActivity.getLayoutInflater().inflate(
				R.layout.popup_message_top_list, null);
		return layout;
	}

	public void updateWindow(MessageTopThirdEntity currentThirdEntity) {
		mSelf2.updateThirdEntity(currentThirdEntity);
		mSelf2.updateThirdBtn(currentThirdEntity);
	}

	class TerminalDataTimeListAdapter extends BaseAdapter implements
			OnItemClickListener {

		private List<MessageTopThirdEntity> mEntities;
		private Activity mActivity;
		private ListView mFirstLv;
		private MessageTopThirdEntity mCurrentFirstEntity;

		public TerminalDataTimeListAdapter(ListView firstLv, Activity activity,
				List<MessageTopThirdEntity> entities,
				MessageTopThirdEntity currentFirstEntity) {
			mActivity = activity;

			mEntities = entities;
			mFirstLv = firstLv;

			mCurrentFirstEntity = currentFirstEntity;
			initmEntitiesSelected();

			mFirstLv.setAdapter(this);
			mFirstLv.setOnItemClickListener(this);
			initSelection(currentFirstEntity);
		}

		/**
		 * 根据btn显示的内容来指定下拉框listview选中的项
		 * 
		 * @param currentFirstEntity
		 */
		private void initSelection(MessageTopThirdEntity currentFirstEntity) {
			if (currentFirstEntity == null) {
				return;
			}
			for (int i = 0; i < mEntities.size(); i++) {
				MessageTopThirdEntity entity = mEntities.get(i);
				if (entity.dateFrom.equals(currentFirstEntity.dateFrom)) {
					mFirstLv.setSelection(i);
					entity.isSelected = true;
				}
			}

		}

		private void initmEntitiesSelected() {
			if (mCurrentFirstEntity == null) {
				return;
			}
			if (mCurrentFirstEntity.isSelected) {
				for (MessageTopThirdEntity entity : mEntities) {
					if (entity.dateFrom.equals(mCurrentFirstEntity.dateFrom)) {
						// Log.i("DATA",
						// "currentFirstEntity.dateFrom="+mCurrentFirstEntity.dateFrom);
						entity.isSelected = true;
					}
				}
			}
		}

		@Override
		public int getCount() {
			
			return mEntities.size();
		}

		@Override
		public Object getItem(int position) {
			
			return mEntities.get(position);
		}

		@Override
		public long getItemId(int position) {
			
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewCache viewCache;
			if (convertView == null) {
				convertView = View.inflate(mActivity,
						R.layout.item_series_books_first_lst, null);
				viewCache = new ViewCache(convertView);
				convertView.setTag(viewCache);

			} else {
				viewCache = (ViewCache) convertView.getTag();
			}

			final MessageTopThirdEntity entity = (MessageTopThirdEntity) getItem(position);

			viewCache.getItemNameTv().setText(entity.name);
			if (entity.isSelected) {
				viewCache.getSelectorLyt().setBackgroundResource(
						R.drawable.ic_first_popup_item_selected_bg);
			} else {
				viewCache.getSelectorLyt().setBackgroundDrawable(null);
			}
			return convertView;
		}

		class ViewCache {
			private View baseView;

			private LinearLayout selectorLyt;
			private TextView itemNameTv;

			public ViewCache(View view) {
				baseView = view;
			}

			public LinearLayout getSelectorLyt() {
				if (selectorLyt == null) {
					selectorLyt = (LinearLayout) baseView
							.findViewById(R.id.lyt_selector);
				}
				return selectorLyt;
			}

			public TextView getItemNameTv() {
				if (itemNameTv == null) {
					itemNameTv = (TextView) baseView
							.findViewById(R.id.tv_item_name);
				}
				return itemNameTv;
			}
		}

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			final MessageTopThirdEntity currentEntity = (MessageTopThirdEntity) getItem(position);
			clearmEntitiesSelected(currentEntity);
			currentEntity.isSelected = true;

			/**
			 * 通知getView()刷新
			 */
			notifyDataSetChanged();
			renderDataLst(currentEntity);

		}

		private void clearmEntitiesSelected(
				final MessageTopThirdEntity currentEntity) {
			for (MessageTopThirdEntity entity : mEntities) {
				if (entity.dateFrom != currentEntity.dateFrom
						&& entity.isSelected) {
					entity.isSelected = false;
				}
			}
		}

		private void renderDataLst(MessageTopThirdEntity currentFirstEntity) {

			if (currentFirstEntity != null) {
				updateWindow(currentFirstEntity);
				TerminalExamineTimePopupView.this.dismiss();
			}

		}

	}

}
